namespace SubhadraSolutions.Sharp.Logging.Adapters.Log4Net
{
    using log4net;
    using log4net.Config;
    using SubhadraSolutions.Sharp.Logging.Contracts;
    using System;
    using System.Collections.Concurrent;
    using System.Diagnostics;

    public sealed class LogFactory
    {
        #region Fields

        private static readonly LogFactory _instance = new LogFactory();

        private readonly ConcurrentDictionary<ILog, ILogger> _loggers =
            new ConcurrentDictionary<ILog, ILogger>();

        #endregion Fields

        #region Constructors

        private LogFactory()
        {
            try
            {
                XmlConfigurator.Configure();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }

        #endregion Constructors

        #region Properties

        public static LogFactory Instance
        {
            get { return _instance; }
        }

        public ILogger DefaultLogger
        {
            get { return GetLogger(string.Empty); }
        }

        #endregion Properties

        #region Methods

        public ILogger GetLogger(string loggerName)
        {
            ILog log = LogManager.GetLogger(loggerName);
            return log == null ? null : _loggers.GetOrAdd(log, key => (new LogAsLoggerAdapter(log)));
        }

        #endregion Methods
    }
}